#include "StdAfx.h"
#include "Settings.h"
#include "input.h"

#define COPYKEY(k1,k2)						\
	{												\
	k1.internalcode = k2.internalcode;	\
	k1.scancode = k2.scancode;				\
	}

extern char settings_subkey[];
static char romdir_default[512] = "c:\\breem\\rom";
static char statedir_default[512] = "c:\\breem\\state";
static char hidir_default[512] = "c:\\breem\\highscore";
static char nvramdir_default[512] = "c:\\breem\\nvram";
static keyconfig_t defaultkeys[MAX_KEYS] =		//default keys
	{
	{I_UP,		SDLK_UP},
	{I_DOWN,		SDLK_DOWN},
	{I_LEFT,		SDLK_LEFT},
	{I_RIGHT,	SDLK_RIGHT},
	{I_B1,		SDLK_z},
	{I_B2,		SDLK_x},
	{I_B3,		SDLK_c},
	{I_B4,		SDLK_a},
	{I_B5,		SDLK_s},
	{I_B6,		SDLK_d},
	{I2_UP,		SDLK_i},
	{I2_DOWN,	SDLK_k},
	{I2_LEFT,	SDLK_j},
	{I2_RIGHT,	SDLK_l},
	{I2_B1,		SDLK_r},
	{I2_B2,		SDLK_t},
	{I2_B3,		SDLK_y},
	{I2_B4,		SDLK_f},
	{I2_B5,		SDLK_g},
	{I2_B6,		SDLK_h},
	{I_COIN1,	SDLK_5},
	{I_COIN2,	SDLK_6},
	{I_COIN3,	SDLK_7},
	{I_TILT,	SDLK_0},
	{I_START1,	SDLK_1},
	{I_START2,	SDLK_2},
	{I_START3,	SDLK_3},
	{I_START4,	SDLK_4},
	{I_SERVICE,	SDLK_7},
	{I_ADVANCE,	SDLK_0},
	{I_AUTOUP,	SDLK_9},
	{I_HRESET,	SDLK_8},
	};

CSettings::CSettings()
{
char curdir[512] = "./";
int i;

//GetCurrentDirectory(512,curdir);
sprintf(romdir_default,"%s/rom",curdir);
sprintf(statedir_default,"%s/state",curdir);
sprintf(hidir_default,"%s/highscore",curdir);
sprintf(nvramdir_default,"%s/nvram",curdir);
for(i=0;i<16;i++)
	dip_default[i] = 0;
for(i=0;i<MAX_KEYS;i++)
	COPYKEY(keyconfig[i],defaultkeys[i]);
SetDefault();
Load();
}

CSettings::~CSettings()
{
Save();
}

void CSettings::SetDefault()
{
int i;

strcpy(romdir,romdir_default);
strcpy(statedir,statedir_default);
strcpy(hidir,hidir_default);
strcpy(nvramdir,nvramdir_default);
for(i=0;i<16;i++)
	dip[i] = dip_default[i];
for(i=0;i<MAX_KEYS;i++)
	COPYKEY(keyconfig[i],defaultkeys[i]);
lockfps = 1;
startupstate = 0;
}

int CSettings::Load()
{
/*
CRegistry key;
int i;
char name[32];

if(key.Open(HKEY_CURRENT_USER,settings_subkey) != ERROR_SUCCESS)
	{
	message("CSettings::Save:  error opening registry key\n");
	return(1);
	}
key.QueryString("ROM Directory",romdir,romdir_default);
key.QueryString("Save State Directory",statedir,statedir_default);
key.QueryString("High Score Directory",hidir,hidir_default);
key.QueryString("NVRAM Directory",nvramdir,nvramdir_default);
key.QueryDWORD("Lock FPS",&lockfps,1);
key.QueryDWORD("Startup State",&startupstate,1);
for(i=0;i<16;i++)
	{
	sprintf(name,"DIP #%d",i);
	key.QueryDWORD(name,&dip[i],dip_default[i]);
	}
for(i=0;i<MAX_KEYS;i++)
	{
	sprintf(name,"KEY #%d Internal Code",i);
	key.QueryDWORD(name,&keyconfig[i].internalcode,defaultkeys[i].internalcode);
	sprintf(name,"KEY #%d Scan Code",i);
	key.QueryDWORD(name,&keyconfig[i].scancode,defaultkeys[i].scancode);
	}
_mkdir(statedir);
key.Close();
*/
return(0);
}

int CSettings::Save()
{
/*
CRegistry key;
int i;
char name[32];

if(key.Create(HKEY_CURRENT_USER,settings_subkey) != ERROR_SUCCESS)
	{
	message("CSettings::Save:  error creating registry key\n");
	return(1);
	}
key.SetString("ROM Directory",romdir);
key.SetString("Save State Directory",statedir);
key.SetString("High Score Directory",hidir);
key.SetString("NVRAM Directory",nvramdir);
key.SetDWORD("Lock FPS",lockfps);
key.SetDWORD("Startup State",startupstate);
for(i=0;i<16;i++)
	{
	sprintf(name,"DIP #%d",i);
	key.SetDWORD(name,dip[i]);
	}
for(i=0;i<MAX_KEYS;i++)
	{
	sprintf(name,"KEY #%d Internal Code",i);
	key.SetDWORD(name,keyconfig[i].internalcode);
	sprintf(name,"KEY #%d Scan Code",i);
	key.SetDWORD(name,keyconfig[i].scancode);
	}
key.Close();
*/
return(0);
}
